using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using MiniTheme.Models;
using MiniTheme.Services.Interfaces;
using MiniTheme.Views.Pages;

namespace MiniTheme.ViewModels;

public partial class MainViewModel : ObservableObject
{
    private readonly IThemeService _themeService;
    private readonly ILanguageService _languageService;
    private readonly INavigationService _navigationService;

    [ObservableProperty]
    private ThemeMode _selectedThemeMode;

    [ObservableProperty]
    private bool _isThemeViewSelected = true;

    [ObservableProperty]
    private bool _isLanguageViewSelected;

    [ObservableProperty]
    private object? _currentView;

    public MainViewModel(IThemeService themeService, ILanguageService languageService, INavigationService navigationService)
    {
        _themeService = themeService;
        _languageService = languageService;
        _navigationService = navigationService;

        _navigationService.CurrentViewChanged += OnNavigationChanged;
        _selectedThemeMode = _themeService.GetCurrentThemeMode();
    }

    private void OnNavigationChanged(object? sender, EventArgs e)
    {
        CurrentView = _navigationService.CurrentView;
    }

    partial void OnIsThemeViewSelectedChanged(bool value)
    {
        if (value)
        {
            NavigateToTheme();
            IsLanguageViewSelected = false;
        }
    }

    partial void OnIsLanguageViewSelectedChanged(bool value)
    {
        if (value)
        {
            NavigateToLanguage();
            IsThemeViewSelected = false;
        }
    }

    [RelayCommand]
    private void NavigateToTheme() => _navigationService.NavigateTo<ThemeView>();

    [RelayCommand]
    private void NavigateToLanguage() => _navigationService.NavigateTo<LanguageView>();

    [RelayCommand]
    private void SetLightTheme()
    {
        SelectedThemeMode = ThemeMode.Light;
        _themeService.ApplyTheme(ThemeMode.Light);
    }

    [RelayCommand]
    private void SetDarkTheme()
    {
        SelectedThemeMode = ThemeMode.Dark;
        _themeService.ApplyTheme(ThemeMode.Dark);
    }

    [RelayCommand]
    private void SetAutoTheme()
    {
        SelectedThemeMode = ThemeMode.Auto;
        _themeService.ApplyTheme(ThemeMode.Auto);
    }

    [RelayCommand]
    private void SetLanguage(string languageCode)
    {
        _languageService.ApplyLanguage(languageCode);
    }
}
